package personal.mhc.pardus.service;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import personal.mhc.pardus.model.PriceGuide;
import personal.mhc.pardus.model.commodities.CommodityImpl;
import personal.mhc.pardus.service.exception.CommodityException;
/**
* Service that provides PriceGuides.
*
* @author Mark Camp
*/
public class PriceGuideService {
/**
* Creates and returns a new PriceGuide.
*
* @return PriceGuide Default PriceGuide.
*/
public PriceGuide newPriceGuide() {
// Return newly created price guide.
return new PriceGuide();
}
/**
* Updates provided commodity in provided priceGuide.
*
* PriceGuide may be null, in which case a new PriceGuide will be created and used.
*
* @param priceGuide PriceGuide that needs updated.
* @param commodity CommodityImpl that needs to be updated in priceGuide.
* @return PriceGuide Updated priceGuide.
* @throws CommodityException Exception thrown when there is a problem with the commodity.
*/
public PriceGuide updatePriceGuide(PriceGuide priceGuide, CommodityImpl commodity) throws CommodityException {
// Check to make sure we have a PriceGuide.
if(priceGuide == null){
// We don't have a PriceGuide, so ...
// Create new PriceGuide.
priceGuide = newPriceGuide();
}
try {
// Get commodity's specific class.
Class<? extends CommodityImpl> classInput2 = commodity.getClass();
// Create parameter array and store the commodity's class.
Class[] paramTypes = new Class[1];
paramTypes[0] = classInput2;
// Get method with desired name and parameter type (ie. set commodity type).
Method method = priceGuide.getClass().getMethod("set"+classInput2.getSimpleName(), paramTypes);
// Invoke method using priceGuide and commodity.
method.invoke((Object)priceGuide, (Object)commodity);
} catch (Exception ex) {
// Log any exceptions that occur.
// Logger.getLogger(PriceGuideService.class.getName()).log(Level.SEVERE, null, ex);
// Throw CommodityException due to problem with commodity.
throw new CommodityException(ex);
}
// Return priceGuide
return priceGuide;
}
}